﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Toasts.Models;
using WebMatrix.WebData;

namespace Toasts.Controllers
{
     [Authorize]
    public class SubmissionsController : ApiController
    {
       // private Repository db = new Repository();

        // GET api/Submission
        public IEnumerable<Submission> GetSubmissions()
        {
            using (Repository db = new Repository())
            {
                return db.Submissions.ToList();
            }
        }

        

        public HttpResponseMessage PutSubmissions(IEnumerable<Submission> submsns)
        {
            using (Repository db = new Repository())
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        //if (WebSecurity.CurrentUserName.Equals("demo") 
                        //{
                        //     db.saveEditedSubmissionsDemo(submsns, Request.UserHostAddress);

                        //} else {

                        //     db.saveEditedSubmissions(submsns);
                        //}

                        db.saveEditedSubmissions(submsns);
                    }
                    catch (Exception)
                    {

                        return Request.CreateResponse(HttpStatusCode.InternalServerError);
                    }
                    return Request.CreateResponse(HttpStatusCode.Accepted);

                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }

        }

      

        //// POST api/Submission
        public HttpResponseMessage PostSubmission(Submission submission)
        {
            if (ModelState.IsValid)
            {
                using (Repository db = new Repository())
                {
                    db.Submissions.Add(submission);
                    db.SaveChanges();

                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, submission);
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = submission.Id }));
                    return response;
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        
    }
}